package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISelectSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.SaturnData;

public class QueryByCondition {
	public List<SaturnData> execute(String fullKey, String condition,
			Connection conn) throws Exception {
		if (condition == null) {
			condition = "";
		}
		condition = " WHERE 1=1 " + condition;
		String dbType = SaturnDataUtil.getStandardDBType(fullKey);
		ISaturnSQLBuilder builder = SaturnSQLBuilderFactory
				.getSaturnSQLBuilder(dbType);
		if (builder != null) {
			ISelectSQLBuilder selectSQLBuilder = builder
					.getSelectSQLBuilder(getBuilderType(fullKey));
			ISaturnSQL saturnSql = selectSQLBuilder.build(fullKey, condition);
			List<SaturnData> list = SaturnSQLHandler.executeQuery(saturnSql, conn);
			return list;
		}
		return null;
	}

	private String getBuilderType(String fullKey) {
		// 如果有大数据
		if (SaturnDataUtil.hasLOB(fullKey)) {
			return ISaturnSQL.SELECT_LOB_OPERATION;
		} else {
			return ISaturnSQL.SELECT_OPERATION;
		}
	}

//	private boolean hasLOB(String fullKey) {
//		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(
//				fullKey);
//		for (String key : dataInfo.keySet()) {
//			IDataItem dataItem = dataInfo.getDataItem(key);
//			if (OracleAttribute.isLOB(dataItem.getType())) {
//				return true;
//			}
//		}
//		return false;
//	}
}
